<?php
require_once("classes/page.class.php");
require_once("classes/functions.php");
require_once("classes/db_functions.php");
require_once("classes/adrese.class.php");


$ugoid = trim(getFromInput("ugovor"));
$obnova = getFromInput("obnova"); $obnova = $obnova=="on"?1:0;
if ($ugoid>0) {
	make($ugoid);
}else{
	form();
}

function form(){
	?>
	<meta http-equiv="content-type" content="text/html; charset=utf8;" />
	<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
		<label for="ugovor">ugovor:</label> <input type="text" name="ugovor" id="ugovor" />
		<input type="submit" value="kreiraj predračun" />
		<br />
		<label for="obnova">obnova:</label><input type="checkbox" name="obnova" id="obnova" />
	</form>
	<?php
}

function make($ugoid){
	global $obnova;
	$page = new page();
	
	$file = trim(implode("",file("http://cvet.yellow/yap1/getpredracun.php?br=$ugoid")));
	
	if ($file == "0") {
		die("Ugovor <strong>$ugoid</strong> ne postoji.");
	}else{
		
		$file = explode("\n",$file);
		foreach ($file as $line){
			$equ = strpos($line,"=");
			$key = substr($line,0,$equ);
			$val = substr($line,$equ+1);
			
			if (preg_match('/_\d+$/',$key)) {
				$under = strrpos($key,"_");
				$index = intval(substr($key,$under+1));
				$varname = substr($key,0,$under);
				$data[$varname][$index] = $val;
			}else{
				$data[$key] = $val;
			}
		}
		
		$page->assign("date",date("d.m.Y"));
		
		if (strlen($ugoid)>5) {
			$page->assign("ugoid",substr($ugoid,0,5)."-".substr($ugoid,5));
		}else{
			$page->assign("ugoid",$ugoid);
		}
		
		$komitent = getKomitent($data["parid"]); //print_rarray($komitent);
		$page->assign("komitent_ime",$komitent["name"]);
		$page->assign("komitent_adresa",$komitent["adresa"]["ulica"]." ".$komitent["adresa"]["broj"]);
		$mesto = $komitent["adresa"]["lokacija"]["mesto"]["name"];
		$ptt = $komitent["adresa"]["lokacija"]["mesto"]["ptt"];
		if (isset($komitent["adresa"]["lokacija"]["opstina"])) {
			$mesto .= ", ".$komitent["adresa"]["lokacija"]["opstina"]["name"];
			if (strlen(trim($komitent["adresa"]["lokacija"]["opstina"]["ptt"]))) {
				$ptt = $komitent["adresa"]["lokacija"]["opstina"]["ptt"];
			}
		}
		if (isset($komitent["adresa"]["lokacija"]["naselje"])) {
			$mesto .= ", ".$komitent["adresa"]["lokacija"]["naselje"]["name"];
			if (strlen(trim($komitent["adresa"]["lokacija"]["naselje"]["ptt"]))) {
				$ptt = $komitent["adresa"]["lokacija"]["naselje"]["ptt"];
			}
		}
		$page->assign("komitent_mesto","$ptt $mesto");
		$page->assign("komitent_pib",$komitent["pib"]);
		$page->assign("komitent_tel",$komitent["tel"]);
		$page->assign("komitent_fax",$komitent["fax"]);
		$page->assign("komitent_mail",$komitent["mail"]);
		$page->assign("komitent_zr",$data["ugoparzr"]);
		
		$sum_artcena = $sum_artpopust = $sum_artporez = $sum_artukupno = 0;
		for ($i=0; $i<count($data["artnaz"]); $i++){
			$count[]	= $i;
			$artname[]	= $data["artnaz"][$i];
			$kol[]		= number_format($data["ugskol"][$i],2,",",".");
			$cena[]		= number_format($data["ugscen"][$i],2,",",".");
			$porez[]	= number_format($data["ugspor"][$i],2,",",".");
			$ukupno[]	= number_format($data["ugscart"][$i],2,",",".");
			$popust[]	= number_format($data["ugspopust"][$i],2,",",".");
			
			$sum_artcena	+= floatval($data["ugscen"][$i]);
			$sum_artpopust	+= floatval($data["ugspopust"][$i]);
			$sum_artporez	+= floatval($data["ugspor"][$i]);
			$sum_artukupno	+= floatval($data["ugscart"][$i]);
		}
		
		$zauplatu = $data["ugobrrat"]>0?$data["ugovrrat"]:0;
		//$zauplatu = 0;
		if ($data["ugoavans"]>0) {
			if ($data["ugoupla"]>0) {
				if ($data["ugoupla"] < $data["ugoavans"]) {
					$zauplatu += $data["ugovrrat"]-$data["ugoupla"];
				}else{
					//$rest = ($data["ugoupla"]-$data["ugoavans"]) % $data["ugovrrat"];
					if ($data["ugodug"]>$data["ugovrrat"]) {
						//$zauplatu
						$rest = $data["ugodug"] % $data["ugovrrat"];
						$zauplatu += $rest;
					}else{
						$zauplatu = $data["ugodug"];
					}
					/*if ($rest>0) {
						$zauplatu += $rest;
					}*/
				}
			}else{
				$zauplatu = $data["ugoavans"];
			}
		}else{
			
		}
		
		$tip_placanja = $data["tnnaz"];
		$placanje_avans = "";
		if ($data["ugoavans"]>0) {
			$avans_perc = round(100*$data["ugoavans"]/$sum_artukupno,0);
			$placanje_avans = "<br />Avans $avans_perc% +";
		}
		
		//print_rarray($data);
		$page->assign("count",$count);
		$page->assign("artname",$artname);
		$page->assign("artkol",$kol);
		$page->assign("artcena",$cena);
		$page->assign("artpopust",$popust);
		$page->assign("artporez",$porez);
		$page->assign("artukupno",$ukupno);
		
		$page->assign("sum_artcena",number_format($sum_artcena,2,",","."));
		$page->assign("sum_artpopust",number_format($sum_artpopust,2,",","."));
		$page->assign("sum_artporez",number_format($sum_artporez,2,",","."));
		$page->assign("sum_artukupno",number_format($sum_artukupno,2,",","."));
		
		$page->assign("osnovica_pdv",number_format(($sum_artcena-$sum_artpopust),2,",","."));
		$page->assign("zauplatu",number_format($zauplatu,2,",","."));
		
		$page->assign("broj_rata",$data["ugobrrat"]);
		$page->assign("broj_rata_txt",brojRataText($data["ugobrrat"]));
		$page->assign("tip_placanja",$tip_placanja);
		$page->assign("placanje_avans",$placanje_avans);
		
		$page->assign("uplaceno",number_format($data["ugoupla"],2,",","."));
		$page->assign("dug",number_format($data["ugodug"],2,",","."));
		$page->assign("avans",number_format($data["ugoavans"],2,",","."));
		$page->assign("vrrate",number_format($data["ugovrrat"],2,",","."));
		
		if ($obnova<1) {
			$page->assign("obnova_comment_start","<!--");
			$page->assign("obnova_comment_end","-->");
		}
		
		echo $page->parseTemplate("predracun.tpl");
	}
}

function getKomitent($id){
	$db	= new db_functions();
	$adr = new adrese();
	
	$res = $db->getOne("SELECT yap FROM yapfox WHERE fox=$id LIMIT 1");
	$id = ($res===null)?$id:$res;
	
	$ret = array();
	
	// basic data
	list($ret["name"],$ret["adrid"],$ret["pib"]) = $db->getRow("SELECT komitent_name, komitent_adresa, komitent_pib FROM komitenti WHERE komitent_id=$id LIMIT 1");
	$ret["adresa"] = $adr->getAll($ret["adrid"]);
	
	// tel, fax
	$ret["tel"] = $db->getOne("SELECT telefon FROM komitenti_telefoni WHERE kom_id=$id AND del_id=0 AND fax=0 ORDER BY telefon LIMIT 1");
	$ret["fax"] = $db->getOne("SELECT telefon FROM komitenti_telefoni WHERE kom_id=$id AND del_id=0 AND fax=1 ORDER BY telefon LIMIT 1");
	
	// email
	$ret["mail"] = $db->getOne("SELECT email FROM komitenti_emails WHERE kom_id=$id AND del_id=0 LIMIT 1");
	
	return $ret;
}
function brojRataText($br){
	$br = intval($br);
	switch ($br){
		case 2:
		case 3:
		case 4:
			$ret = "rate";
			break;
		default:
			$ret = "rata";
			break;
	}
	return $ret;
}
?>